package com.his.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.his.dao.ServerFeeDAO;
import com.his.dao.domain.ServerCategory;
import com.his.dao.domain.ServerProject;
import com.his.struts.util.CollectionUtils;
import com.his.struts.util.DbUtil;

public class ServerFeeDAOImpl implements ServerFeeDAO {

	Logger log = Logger.getLogger(ServerFeeDAOImpl.class);
	DbUtil db = new DbUtil();

	@Override
	public boolean saveServerCategory(ServerCategory sc) {
		String sql = "insert into server_category (name,parentId,status) values(?,?,?)";
		List<Object> params = sc2List(sc);
		return write(sql, params);
	}

	private boolean write(String sql, List<Object> params) {
		int x = 0;
		try {
			x = db.executeUpdate(sql, params);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			db.closeAll();
		}
		return x == 1;
	}

	private List<Object> sc2List(ServerCategory sc) {
		List<Object> params = new ArrayList<Object>();
		params.add(sc.getName());
		params.add(sc.getParentId());
		params.add(sc.getStatus());
		return params;
	}

	private List<Object> sp2List(ServerProject sp) {
		List<Object> params = new ArrayList<Object>();
		params.add(sp.getName());
		params.add(sp.getContent());
		params.add(sp.getExclude());
		params.add(sp.getUnit());
		params.add(sp.getPrice());
		params.add(sp.getRemarks());
		params.add(sp.getCategoryId());
		params.add(sp.getStatus());
		return params;
	}

	@Override
	public boolean updateServerCategory(ServerCategory sc) {
		String sql = "update server_category set name=?,parentId=?,status=? where id=?";
		List<Object> params = sc2List(sc);
		params.add(sc.getId());
		return write(sql, params);
	}

	@Override
	public boolean saveServerProject(ServerProject sp) {
		String sql = "insert into server_project (name,content,exclude,unit,price,remarks,categoryId,status) values (?,?,?,?,?,?,?,?)";
		List<Object> params = sp2List(sp);
		return write(sql, params);
	}

	@Override
	public boolean updateServerProject(ServerProject sp) {
		String sql = "insert into server_project set name=?,content=?, exclude=?,unit=?,price=?,remarks=?,categoryId=?,status=? where id= ?";
		List<Object> params = sp2List(sp);
		params.add(sp.getId());
		return write(sql, params);
	}

	private ServerCategory scRs2Domain(ResultSet rs) throws SQLException {
		ServerCategory sc = new ServerCategory();
		sc.setId(rs.getInt("id"));
		sc.setName(rs.getString("name"));
		sc.setParentId(rs.getInt("parentId"));
		sc.setStatus(rs.getInt("status"));
		return sc;
	}

	@Override
	public List<ServerCategory> queryAllRootCategory(boolean needSub) {
		String sql = "select * from server_category where parentId is null";
		List<ServerCategory> result = null;
		try {
			ResultSet rs = db.executeQuery(sql, null);
			if (null != rs) {
				result = new ArrayList<ServerCategory>();
				while (rs.next()) {
					ServerCategory sc = scRs2Domain(rs);
					result.add(sc);
				}
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			db.closeAll();
		}
		return needSub ? getSub(result) : result;
	}

	private List<ServerCategory> getSub(List<ServerCategory> scList) {
		for (ServerCategory sc : scList) {
			List<ServerCategory> list = this.queryCategoryByParentId(sc
					.getParentId());
			if (CollectionUtils.isNotEmpty(list)) {
				sc.setSubSc(list);
				this.getSub(list);
			}
		}
		return scList;
	}

	public List<ServerCategory> queryCategoryByParentId(int parentId) {
		String sql = "select * from server_category where parentId=?";
		List<Object> params = new ArrayList<Object>();
		params.add(parentId);
		List<ServerCategory> result = null;
		try {
			ResultSet rs = db.executeQuery(sql, null);
			if (null != rs) {
				result = new ArrayList<ServerCategory>();
				while (rs.next()) {
					ServerCategory sc = scRs2Domain(rs);
					result.add(sc);
				}
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			db.closeAll();
		}
		return result;
	}

	@Override
	public List<ServerProject> queryProjectByCategoryId(int categoryId) {
		String sql = "select * from server_project where categoryId=?";
		List<Object> params = new ArrayList<Object>();
		params.add(categoryId);
		List<ServerProject> result = null;
		try {
			ResultSet rs = db.executeQuery(sql, params);
			if (null != rs) {
				result = new ArrayList<ServerProject>();
				while (rs.next()) {
					ServerProject sp = spRs2Domain(rs);
					result.add(sp);
				}
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			db.closeAll();
		}
		return null;
	}

	private ServerProject spRs2Domain(ResultSet rs) throws SQLException {
		ServerProject sp = new ServerProject();
		sp.setCategoryId(rs.getInt("categoryId"));
		sp.setContent(rs.getString("content"));
		sp.setExclude(rs.getString("exclude"));
		sp.setId(rs.getInt("id"));
		sp.setName(rs.getString("name"));
		sp.setPrice(rs.getLong("price"));
		sp.setRemarks(rs.getString("remarks"));
		sp.setStatus(rs.getInt("status"));
		sp.setUnit(rs.getString("unit"));
		return sp;
	}

	@Override
	public boolean deleteProjectByCategoryId(int categoryId) {
		String sql = "update server_project set status= ? where categoryId= ?";
		List<Object> params = new ArrayList<Object>();
		params.add(1);
		params.add(categoryId);
		return this.write(sql, params);
	}

	@Override
	public boolean deleteProjectById(int projectId) {
		String sql = "update server_project set status=? where id=?";
		List<Object> params = new ArrayList<Object>();
		params.add(1);
		params.add(projectId);
		return this.write(sql, params);
	}

	@Override
	public boolean deleteCategoryByParentId(int parentId) {
		String sql = "update server_category set status=? where parentId=?";
		List<Object> params = new ArrayList<Object>();
		params.add(1);
		params.add(parentId);
		return this.write(sql, params);
	}

	@Override
	public boolean deleteCategoryById(int categoryId) {
		String sql = "update server_category set status=? where id= ?";
		List<Object> params = new ArrayList<Object>();
		params.add(1);
		params.add(categoryId);
		return this.write(sql, params);
	}

}
